#include<iostream>
#include<thread>
#include<future>

//通过在线程中对promise对象设置数据， 其他线程通过future获取设置数据的方式实现获取异步执行结果的功能
void Add(int num1, int num2, std::promise<int> &prom)
{
    // std::cout << "add 111" << std::endl;
    std::this_thread::sleep_for(std::chrono::seconds(5));
    // std::cout << "add 222" << std::endl;
    prom.set_value(num1 + num2);
    std::this_thread::sleep_for(std::chrono::seconds(5));

    return;
}


int main()
{
    std::promise<int> prom;
    std::future<int> fu = prom.get_future();    //绑定
    
    std::thread thr(Add, 22, 33, std::ref(prom));
    std::cout << "-----------1-------------" << std::endl;
    int result = fu.get();                      //没得到结果就会等
    std::cout << "sum: " << result << std::endl;     
    thr.join();
    return 0;
}